<?php

namespace App\Admin\Controllers\Content;

use App\Models\Content\Article;
use App\Models\Content\ArticleCategory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;

class ArticleController extends AdminController
{
    public function __construct()
    {
        Admin::translator()->setPath("content");
    }

    public function title()
    {
        return admin_trans_label("article");
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = Article::orderBy("updated_at","desc");
        $model->with("category");
        $category = ArticleCategory::selectOptions();
        $category["0"] = "无分类";
        return Grid::make($model, function (Grid $grid) use($category){

            $grid->setActionClass(\Dcat\Admin\Grid\Displayers\Actions::class);
            $grid->showViewButton(false);
            $grid->filter(function (Grid\Filter $filter) use($category){
                $filter->panel();
                //如果存在条件就展开搜索框，否则点击才会显示
                $filter->expand(true);
                $filter->like('title')->width(2);
                $filter->equal('key')->width(2);
                $filter->equal('category_id')->select($category)->width(2);
            });


            $grid->column('id')->width(100)->sortable();
            $grid->column('key')->width(120)->editable();
            $grid->column('title')->limit(100)->width(600);
            $grid->column('category_id',admin_trans_field("category"))->select($category)->width(250);
            $grid->column('views')->width(100);
            $grid->column('likes')->width(100);
            $grid->column('state')->width(100)->radio(Article::selectOptions());
            $grid->column('sort')->sortable()->orderable();
            $grid->column('publish_at')->width(175);
            $grid->column('created_at')->width(175);
            $grid->column('updated_at')->width(175)->sortable();

            $url = admin_url("article-category");
            #增加按钮
            $grid->tools(<<<HTML
                <div class="pull-left" data-responsive-table-toolbar="grid-table" style="margin:0 5px">
                    <a href="{$url}" class="btn btn-primary"><i class="feather icon-layers"></i><span class="d-none d-inline">&nbsp;&nbsp;文章分类</span></a>
                 </div>
HTML
            );
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Article(), function (Form $form) {
            /*
            //限制创建者才可以编辑文章
            if (!Gate::forUser(Admin::user())->allows('update',$form->model())){
                abort(403);
            }*/
            #$form->disableViewButton();
            $form->display('id');
            $form->text('title')->required();
            $form->text('key')->required()->maxLength(32)->default(uniqid())->help("指定一个别名id，用于seo的路由");
            #$form->number('sort')->default(0);
            $form->select('category_id')->default(0)->options(array_merge(["0"=>"无分类"],ArticleCategory::selectOptions()))->help("如果不需要可以不选择分类");
            $form->image('picture')->autoUpload()->removable()->width(4)
                ->accept('jpg,png,gif,jpeg', 'image/*')
                ->disk("public");
            $form->editor('content')->required();
            #$form->radio('state')->default(0)->options(Article::selectOptions())->help("文章的当前状态，草稿 和下线 的文章不会展示");

            $form->embeds("seo",function ($form){
                $form->text('title');
                $form->textarea('keywords');
                $form->textarea('description');
            })->saving(function ($v){
                return json_encode($v,JSON_UNESCAPED_UNICODE);
            });

            $form->hidden("admin_id")->disable();
            #$form->display('created_at');
            #$form->display('updated_at');

            $form->saving(function (Form $form){
                #记录创建人
                if($form->isCreating()){
                    $form->input("admin_id",Admin::user()->getAuthIdentifier());
                }
                if(!$form->category_id){
                    $form->input("category_id",0);
                }
            });
        });
    }

    public function match()
    {
        $q = request()->get("q");
        $articles =  Article::where("title","like","%$q%")->paginate(null,["id","key","title as text"]);
        foreach ($articles->items() as &$item){
            $item["text"] = Str::limit($item["text"],30);
        }
        return $articles;
    }
}
